﻿using EasyTest.Shared.EnumType;
using EasyTest.Shared.TestAttribute;
using EasyTest.Shared.外部访问;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using ZLGCANoptimize.方法;

namespace ZLGCANoptimize
{
    public class 屏
    {
        //static ZLGCANHelper zLGCANHelper = null;
        //static List<ZCAN_Receive_Data> zCAN_Receive_Datas = null;

//        [ExposedMethodAttribute("")]
//        public bool 设置屏颜色(int 通道, string 颜色, out string 信息, out string data)
//        {
//#if ELO20240927
//            data = "";
//            try
//            {
//                string 指令 = "";
//                switch (颜色)
//                {
//                    case "白屏":
//                    case "白": 指令 = "04 2E 4C 40 01 00 00 00"; break;
//                    case "红屏":
//                    case "红": 指令 = "04 2E 4C 40 03 00 00 00"; break;
//                    case "绿屏":
//                    case "绿": 指令 = "04 2E 4C 40 04 00 00 00"; break;
//                    case "蓝屏":
//                    case "蓝": 指令 = "04 2E 4C 40 05 00 00 00"; break;
//                    case "黑屏":
//                    case "黑": 指令 = "04 2E 4C 40 02 00 00 00"; break;
//                    default:   指令 = "04 2E 4C 40 FF 00 00 00"; break;  //默认使用退出指令
//                }

//                List<CanCmdModel> cmdList = new List<CanCmdModel>();
//                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = 指令 });
//                CanReadRequestModel requestModel = new CanReadRequestModel();
//                requestModel.Channel = (ushort)通道;
//                requestModel.IsClearCache = true;
//                requestModel.IsDecode = true;
//                requestModel.DecodeLevel = "62";
//                requestModel.CmdList = cmdList;
//                requestModel.SendCmdTimeSpan = 50;
//                requestModel.ReceiveCmdTimeSpan = 2000;
//                requestModel.MaxSendCount = 5;
//                requestModel.AutoParse = false;
//                requestModel.IsVerifyDid = true;
//                requestModel.RequestMode = CanRequestMode.发送并接收;

//                var 发送响应 = 数据.发送指令(requestModel);
//                if (发送响应.Success == false)
//                {
//                    信息 = 发送响应.ErrorMessage;
//                    data = string.Join(",", 发送响应.ResultList ?? new List<string>());
//                    return false;
//                }

//                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 40", out string 目标值);
//                if (!提取结果)
//                {
//                    信息 = "设置屏颜色失败";
//                    data = string.Join(",", 发送响应.ResultList ?? new List<string>());
//                    return false;  //指定数据帧不存在
//                }

//                信息 = "设置屏颜色成功";
//                data = string.Join(",", 发送响应.ResultList ?? new List<string>());

//                return true;
//            }
//            catch (Exception ex)
//            {
//                信息 = "设置屏颜色失败" + ex.Message;
//                //LogHelper.Error($"设置屏颜色异常，{ex.Message}，{ex.StackTrace}");
//                eLog.AddLog($"设置屏颜色异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
//            }
//            return false;
//#else
//            data = "";
//            try
//            {
//                string 指令 = "";
//                switch (颜色)
//                {
//                    case "白屏":
//                    case "白": 指令 = "10 2E 4C 40 01 00 00 00"; break;
//                    case "红屏":
//                    case "红": 指令 = "10 2E 4C 40 03 00 00 00"; break;
//                    case "绿屏":
//                    case "绿": 指令 = "10 2E 4C 40 04 00 00 00"; break;
//                    case "蓝屏":
//                    case "蓝": 指令 = "10 2E 4C 40 05 00 00 00"; break;
//                    case "黑屏":
//                    case "黑": 指令 = "10 2E 4C 40 02 00 00 00"; break;
//                    default:   指令 = "10 0C 2E 4C 40 FF 00 00"; break;  //默认使用退出指令
//                }

//                List<CanCmdModel> cmdList = new List<CanCmdModel>();
//                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = 指令 });
//                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = "21 00 00 00 00 00 00 00" });
//                CanReadRequestModel requestModel = new CanReadRequestModel();
//                requestModel.Channel = (ushort)通道;
//                requestModel.IsClearCache = true;
//                requestModel.IsDecode = true;
//                requestModel.DecodeLevel = "62";
//                requestModel.CmdList = cmdList;
//                requestModel.SendCmdTimeSpan = 50;
//                requestModel.ReceiveCmdTimeSpan = 2000;
//                requestModel.MaxSendCount = 5;
//                requestModel.AutoParse = false;
//                requestModel.IsVerifyDid = true;
//                requestModel.RequestMode = CanRequestMode.发送并接收;

//                var 发送响应 = 数据.发送指令(requestModel);
//                if (发送响应.Success == false)
//                {
//                    信息 = 发送响应.ErrorMessage;
//                    data = string.Join(",", 发送响应.ResultList ?? new List<string>());
//                    return false;
//                }

//                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 40", out string 目标值);
//                if (!提取结果)
//                {
//                    信息 = "设置屏颜色失败";
//                    data = string.Join(",", 发送响应.ResultList ?? new List<string>());
//                    return false;  //指定数据帧不存在
//                }

//                信息 = "设置屏颜色成功";
//                data = string.Join(",", 发送响应.ResultList ?? new List<string>());

//                return true;
//            }
//            catch (Exception ex)
//            {
//                信息 = "设置屏颜色失败" + ex.Message;
//                LogHelper.Error($"设置屏颜色异常，{ex.Message}，{ex.StackTrace}");
//            }
//            return false;
//#endif
//        }


        //=================================== 2024-09-27 ELO指令更新，以下方法为对应更新的内容 ==========================================
        [ExposedMethodAttribute("")]
        public bool 设置屏颜色4C40(int 通道, string 颜色, out string 信息, out string data)
        {
            data = "";
            try
            {
                /*
                 * TFT display pattern={
                    0xFF: back to home page
                    0x01: White Screen
                    0x01: White Screen
                    0x02: Black Screen
                    0x03: Red Screen
                    0x04: Green Screen
                    0x05: Blue Screen
                    0x06: 8 Color bar Screen
                    0x07: Big chess Screen
                    0x08: fish screen
                    0x09: Gray black flicker  Screen
                    0x0A: H Gray Scale Screen
                    0x0B: Mid Grey Screen
                    0x0C: V BW Screen
                    0x0D: V Gray Scale Screen
                    0x0E: reserved
                    0x0F: reserved
                    }
                 */
                string 指令 = "";
                string str= "ff";
                switch (颜色)
                {
                    case "白屏":
                    case "白": 指令 = "04 2E 4C 40 01 00 00 00"; break;
                    case "红屏":
                    case "红": 指令 = "04 2E 4C 40 03 00 00 00"; break;
                    case "绿屏":
                    case "绿": 指令 = "04 2E 4C 40 04 00 00 00"; break;
                    case "蓝屏":
                    case "蓝": 指令 = "04 2E 4C 40 05 00 00 00"; break;
                    case "黑屏":
                    case "黑": 指令 = "04 2E 4C 40 02 00 00 00"; break;
                    default:
                        if (颜色.Length== str.Length)
                        {
                            指令 = $"04 2E 4C 40 {颜色} 00 00 00"; break;  //默认使用退出指令
                        }
                        指令 = $"04 2E 4C 40 FF 00 00 00"; break;  //默认使用退出指令
                }
                if (颜色==null || 颜色=="")
                {
                    指令 = "04 2E 4C 40 FF 00 00 00";   //默认使用退出指令
                }
               

                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = 指令 });
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = true;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 2000;
                requestModel.MaxSendCount = 5;
                requestModel.AutoParse = false;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    data = string.Join(",", 发送响应.ResultList ?? new List<string>() { "" });
                    return false;
                }

                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 40", out string 目标值);
                if (!提取结果)
                {
                    信息 = "设置屏颜色失败";
                    data = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;  //指定数据帧不存在
                }

                信息 = "设置屏颜色成功";
                data = string.Join(",", 发送响应.ResultList ?? new List<string>());

                return true;
            }
            catch (Exception ex)
            {
                信息 = "设置屏颜色失败" + ex.Message;
                eLog.AddLog($"设置屏颜色异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }
    }
}
